using System;
using System.Linq;
using System.Runtime.InteropServices.Automation;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using ContosoDashboard.ContosoDashboardService;

namespace ContosoDashboard.Views
{
    public partial class Customer : Page
    {
        private CustomerInfo _customer;

        public Customer()
        {
            InitializeComponent();
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            var queryString = e.Uri.OriginalString.Substring(e.Uri.OriginalString.IndexOf('?') + 1);
            var sCustomerId = queryString.Substring("customerId=".Length);
            var customerId = long.Parse(sCustomerId);

            _customer = ReactiveContosoService.Current.Customers.Single(it => it.Id == customerId);

            DataContext = _customer;

            // browser.Navigate(new Uri("http://www.bing.com/maps/?where=" + _customer.City));
            browser.Navigate(new Uri(@"http://maps.googleapis.com/maps/api/staticmap?center="+_customer.City+@"&zoom=14&size=800x600&sensor=false"));
        }

        private void BtnSendEmailClick(object sender, RoutedEventArgs e)
        {
            if (AutomationFactory.IsAvailable == false)
                return;

            dynamic outlook = AutomationFactory.CreateObject("Outlook.Application");
            dynamic mail = outlook.CreateItem(0);
            mail.To = _customer.Email;
            mail.Subject = "About your Dixel";
            mail.Body = "We have a Dixel for you !";
            mail.Display(); 
        }
    }
}